package com.netistate.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.netistate.action.ProjectAction;

public class MailUtil {
	
	//读取properties文件获取邮件的信息，并赋值。
	private static String AuthenticationAddress = new String();
	private static String AuthenticationPassword = new String();
	private static String MailFromAddress = new String();
	private static String MailTheme = new String();
	static{
		Properties properties = new Properties();
		try {
			properties.load(ProjectAction.class.getClassLoader().getResourceAsStream("mail.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		AuthenticationAddress = properties.getProperty("AuthenticationAddress");
		AuthenticationPassword = properties.getProperty("AuthenticationPassword");
		MailFromAddress = properties.getProperty("MailFromAddress");
		MailTheme = properties.getProperty("MailTheme");
	}

	public static void sendMail(String mailAddress,String subject,String msg){
		
		Email mail = new SimpleEmail();
		mail.setHostName("smtp.netiunite.com");
		mail.setAuthentication(AuthenticationAddress, AuthenticationPassword);
		mail.setCharset("utf-8");
		mail.setTLS(true);
		
		try {
			mail.setFrom(MailFromAddress,MailTheme);
			//邮件的主体
			mail.setSubject(subject);
			//邮件的内容
			mail.setMsg(msg);
			//邮件的接收者
			mail.addTo(mailAddress);
			
			mail.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
}
